﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using SdmxMl.Helper;
using SdmxMl.Common;
using System.Text;
using System.Windows.Forms;

namespace SdmxMl.Panels
{
    public partial class PanelAttributes : PanelBase
    {
        private UserControl visibleView;
        public PanelAttributes()
        {
            InitializeComponent();
            visibleView = null;
        }

        public void BindFromNode(TreeNode node, PanelAnnotationList panelAnnotation)
        {
            // Determines IsFinal state for ReadOnly Operations
            visibleView = null;
            bool isFinal = false;
            if (node != null)
            {
            Artefact a =  node.Tag as Artefact;
                if (a == null)
                    a = TreeHelper.GetParentArtefact(node);
                if (a != null)
                    isFinal = a.IsFinal;
            }

            // Support for Annotations
            if (node != null)
                panelAnnotation.BindFromNode(node.Tag, isFinal);
            else
                panelAnnotation.BindFromNode(null, isFinal);

            // Bind User Controls, The targeted one will become visible
            if (ucArtefactAttributes1.BindFromNode(node, panelAnnotation, isFinal))
                visibleView = ucArtefactAttributes1;

            if (ucCode.BindFromNode(node, isFinal))
                visibleView = ucCode;

            if (ucOrganisation.BindFromNode(node, isFinal))
                visibleView = ucOrganisation;

            if (ucConcept.BindFromNode(node, isFinal))
                visibleView = ucConcept;

            if (ucCategory.BindFromNode(node, isFinal))
                visibleView = ucCategory;
            
            if (ucProcessStep.BindFromNode(node, isFinal))
                visibleView = ucProcessStep;

            if (ucCategorisation.BindFromNode(node, isFinal))
                visibleView = ucCategorisation;

            if (ucKeyFamilyGrid.BindFromNode(node, panelAnnotation, isFinal))
                visibleView = ucKeyFamilyGrid;

            if (ucReportStructure.BindFromNode(node, panelAnnotation, isFinal))
                visibleView = ucReportStructure;

            ucGroup.BindFromNode(node, panelAnnotation, isFinal);
            ucDataflow.BindFromNode(node);
            if (ucHierarchyClRef.BindFromNode(node, isFinal))
                visibleView = ucHierarchyClRef;

            ucHierarchyCl.BindFromNode(node, panelAnnotation, isFinal);

            if (ucMetadataSetType.BindFromNode(node, panelAnnotation, isFinal))
                visibleView = ucMetadataSetType;

            if (ucAttributeValueSet.BindFromNode(node, panelAnnotation, isFinal))
                visibleView = ucAttributeValueSet;

            ucMetadataflow.BindFromNode(node);
            ucTargetIdentifiers.BindFromNode(node, panelAnnotation, isFinal);
            ucDatasetType.BindFromNode(node);
            ucDataGroup.BindFromNode(node, panelAnnotation);
            ucStructureSets.BindFromNode(node, panelAnnotation, isFinal);
            ucReportingTaxonomy.BindFromNode(node, panelAnnotation, isFinal);
        }

        public bool UpdateNodeTag(TreeNode node)
        {
            bool error = false;
            if (ucArtefactAttributes1.UpdateNodeTag(node))
                error = true;
            if (ucConcept.UpdateNodeTag(node))
                error = true;
            if (ucOrganisation.UpdateNodeTag(node))
                error = true;
            if (ucCode.UpdateNodeTag(node))
                error = true;
            if (ucCategory.UpdateNodeTag(node))
                error = true;
            if (ucKeyFamilyGrid.UpdateNodeTag(node))
                error = true;
            if (ucGroup.UpdateNodeTag(node))
                error = true;
            if (ucDataflow.UpdateNodeTag(node))
                error = true;
            if (ucHierarchyClRef.UpdateNodeTag(node))
                error = true;
            if (ucHierarchyCl.UpdateNodeTag(node))
                error = true;
            if (ucReportStructure.UpdateNodeTag(node))
                error = true;
            if (ucMetadataSetType.UpdateNodeTag(node))
                error = true;
            if (ucAttributeValueSet.UpdateNodeTag(node))
                error = true;
            if (ucMetadataflow.UpdateNodeTag(node))
                error = true;
            if (ucTargetIdentifiers.UpdateNodeTag(node))
                error = true;
            if (ucDatasetType.UpdateNodeTag(node))
                error = true;
            if (ucDataGroup.UpdateNodeTag(node))
                error = true;
            if (ucStructureSets.UpdateNodeTag(node))
                error = true;
            if (ucReportingTaxonomy.UpdateNodeTag(node))
                error = true;
            if (ucProcessStep.UpdateNodeTag(node))
                error = true;
            return error; // true on error
        }

        #region  URN of Artefact item

        public string GetArtefactItemUrn()
        {
            return string.Empty;
        }

        public void SetSelectedItemFromUrn(UrnParser up)
        {
            IUrnSelect iurs = visibleView as IUrnSelect;
            if (iurs != null)
                iurs.SelectItem(up);

        }


        #endregion

    }
}
